<?

class CALNODE extends OBJECT {
	function __construct( $type_or_vars, $id = 0, $create_new = false, $vars = false ) {
		
		if( is_array( $type_or_vars ) || is_object( $type_or_vars )){

				$this->_addvars( $type_or_vars );
		} else 	$this->type = $type_or_vars;
		
		if( $vars ) $this->_addvars( $vars );
		
#		Load assigned object or most recent
		if( $id || !$create_new )
			$this->load( $this->type, $this->id = ( int )( $id )? $id : $this->id );

#		Create new
		else
			$this->create( $this->type, $type_or_vars );


#		Set up object Class
		$this->obj		= $this->parse_object();
		$this->loc		= $this->parse_location();
		$this->media	= new OBJECT_MEDIA( $this->type, $this->values[ 'ID' ]);
		
		// DEPRECATED!
		//$this->object = $this->has_object();
	}
	

#		Create Calendar node.
#		If additional string is given, it becomes the name, an array is considered a object referral.	
	function create( $type, $vars, $id = 0 ) {
		global $DB, $Engine;
		
		$nameid = ( is_string( $vars ))? $Engine->text->set( $vars ) : 0;
		$object = ( is_string( $vars ) || !$vars->otype )? (object) array( "otype"=>0, "objid"=>0 ) : $vars;
		
		$DB->query( "insert into ". $DB->type( $type, "table" ) ."( type, otype, date, nameid, objid, active, span, flog, creator ) values ( ". $DB->type( $type ) .", {$object->otype}, now(), $nameid, {$object->objid}, 0, 60, now(), '". $Engine->user_get( 'ID' ). "' )" );
		$this->id = (int) $DB->get_key( $type, "!active", "max(ID)" );
		
		return $this->load( $type, $this->id );
	}
	
	public function load( $type, $id ) {
		global $DB;
		
#		Get DB row & decode meta
		if( $this->datestr ) $tail = ", date_format(date, '{$this->datestr}') as datestr";
		if( $this->timestr ) $tail.= ", date_format(date, '{$this->timestr}') as timestr";
		
		$this->id = $id;
		$this->values = $DB->get( $type, ( $id )? $id : "active order by ID desc", "*, dayofweek( date ) as day, <%lang:nameid%> as name$tail" );
		$this->values [ 'meta' ] = json_decode( $this->values[ 'meta' ]);
		
		return $this->values;
	}
	
	//public function get( $what ) {

		/*if( !$this->values[ $what ] ) {
			switch( $what ) {
				case "content" :
					$column = "<%lang:mod_object.textid:long%>";
					break;
				default :
					$column = $what;
			}
			global $DB;
			$this->values[ $what ] = $DB->get_key( $this->type, (int) $this->values[ "ID" ], $column );
		}*/
		
	//	return $this->values[ $what ];
	//}
	
	public function reference( $objtype, $multi = 1 ) {
		global $DB;
				
		return $results;
	}

#	Related nodes.
	public function related( $spantype = "minute", $datestr = "%d.%m %Y", $timestr = "%H.%i" ) {
		global $DB;	
		
		if( $dates = $DB->filter_key( "cal.single", "date >= '". $this->get( 'date' ) ."' && date <= '". $this->get( 'date' ) ."' + interval ". $this->get( 'span' ) ." $spantype && active order by date", "ID, type" ))
			foreach( $dates as $date ) $result[] =  new CALNODE( array( "type"=> $DB->type_r( $date[ 'type' ]), "id"=> (int) $date['ID'], "datestr"=> $datestr, "timestr"=> $timestr ));
		
		return $result;
	}
	
	private function parse_object() {
	
		return ( $id = $this->get( "objid" ))?
			new OBJECT(( int ) $this->get( "otype" ), ( int ) $id ) : false;
	}

	private function parse_location() {
	
		return ( $id = $this->get( "location" ))?
			new OBJECT( "location", ( int ) $id ) : false;
	}
	
	private function has_object() {
		global $DB;
		
		return ( $id = (int) $this->get( 'objid' ))?
			new OBJECT( $DB->type_r( $this->get( 'otype' )), $id) : (object) array();
		
	}
	
	public function smartday() {
		
		if(( int ) date( 'H', $uts = $this->timestamp()) < 6 )
			$uts = strtotime( $this->get('date') . " -1 day" );
				
		return date( 'Y-m-d', $uts );
	}
	
	public function timestamp() {
	
		return strtotime( $this->get('date'));
	
	}
	
	public function endstamp() {
	
		return strtotime( $this->get('date') . " +" . $this->get('span') . " minute");
	
	}
	
	// $Object->date( '%w dmY', ' - %w dmY', 1 );
	
	public function date( $str, $endstr = false, $smart = false )	{
		
		if( $smart )
			$this->values[ 'date' ] = $this->smartday();
		
		$string = date( $str, $this->timestamp() );
		if( $endstr )
			$string .= date( $endstr, $this->endstamp() );
			
		$days = array( "zo","ma","di","wo","do","vr","za" );

		if( strpos( $string, "%" ) !== false ) {
			//function pregfunc( $prs ){ global $CAL; return $CAL->days[ LANG ][ $prs[ 0 ]{ 1 }]; }
			$string = preg_replace_callback( "/%\d/", create_function( '$prs', 'global $CAL; return $CAL->days[ LANG ][ $prs[ 0 ]{ 1 }];' ), $string );
		}
	
		return $string;
	}
			
}

?>